package com.fhao.principle.controller;

import com.fhao.principle.model.Dog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DogController {
    @Autowired
    private ApplicationContext context;

    @Autowired
    private Dog singletonDog;

    @Autowired
    private Dog prototypeDog;//注入的对象,拿的都是同一个,用context拿bean对象就不一定,要看单例还是多例

    @Autowired
    private Dog requestDog;

    @Autowired
    private Dog sessionDog;

    @Autowired
    private Dog applicationDog;

    @RequestMapping("/single")
    public String single() {
        //从context获取对象
        Dog contextDog = context.getBean("singletonDog",Dog.class);
        return "contextDog:"+contextDog + "   ,autowiredDog:"+singletonDog;
    }

    @RequestMapping("/prototype")
    public String prototype() {
        //从context获取对象
        Dog contextDog1 = context.getBean("prototypeDog",Dog.class);
        Dog contextDog2 = context.getBean("prototypeDog",Dog.class);
        return "contextDog1: "+contextDog1 + "<br/> autowiredDog: "+prototypeDog + "<br/> contextDog2: "+contextDog2;
    }

    @RequestMapping("/request")
    public String request() {
        //从context获取对象
        Dog contextDog1 = context.getBean("requestDog",Dog.class);
        Dog contextDog2 = context.getBean("requestDog",Dog.class);
        return "contextDog1: "+contextDog1 + "<br/> autowiredDog: "+requestDog +"<br/> contextDog2: "+contextDog2;
    }

    @RequestMapping("/session")
    public String session() {
        //从context获取对象
        Dog contextDog1 = context.getBean("sessionDog",Dog.class);
        Dog contextDog2 = context.getBean("sessionDog",Dog.class);
        return "contextDog1: "+contextDog1 + "<br/> autowiredDog: "+sessionDog + "<br/> contextDog2: "+contextDog2;
    }

    @RequestMapping("/application")
    public String application() {
        //从context获取对象
        Dog contextDog1 = context.getBean("applicationDog",Dog.class);
        Dog contextDog2 = context.getBean("applicationDog",Dog.class);
        return "contextDog1: "+contextDog1 + "<br/> autowiredDog: "+applicationDog + "<br/> contextDog2: "+contextDog2;
    }

}



















